package com.zhou.code;

import com.zhou.code.template.ClassDescription;
import com.zhou.code.template.DataModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class DataReader implements Reader {

    private String driverName;
    private String url;
    private String username;
    private String password;
    private Connection connection;

    public DataReader (Properties properties) throws Exception {
        this.driverName = properties.getProperty(Constant.JDBC_DRIVER_NAME_KEY);
        this.url = properties.getProperty(Constant.JDBC_URL_KEY);
        this.username = properties.getProperty(Constant.JDBC_USERNAME_KEY);
        this.password = properties.getProperty(Constant.JDBC_PASSWORD_KEY);
        Class.forName(driverName);
        this.connection = DriverManager.getConnection(url, username, password);
    }

    public DataModel read(String targetName) throws SQLException {
        String sql = "select COLUMN_NAME, DATA_TYPE, CHARACTER_OCTET_LENGTH, COLUMN_COMMENT from `information_schema`.COLUMNS where TABLE_SCHEMA = '" + getDataBaseName() +"' and TABLE_NAME = '" + targetName + "'";
        try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
            List<ColumnDescription> list = new LinkedList<>();
            Map<String, ClassDescription> importClass = new HashMap<>();
            while (resultSet.next()){
                ColumnDescription description = new ColumnDescription();
                description.columnName = resultSet.getString("COLUMN_NAME");
                description.dataType = resultSet.getString("DATA_TYPE");
                Class javaClass = ColumnUtil.dataTypeToClass(description.dataType);
                description.jdbcType = ColumnUtil.dataTypeToJdbcType(description.dataType);
                description.javaType = javaClass.getName();
                description.javaTypeEasy = ColumnUtil.getRealName(javaClass);
                description.length = resultSet.getInt("CHARACTER_OCTET_LENGTH");
                description.columnComment = resultSet.getString("COLUMN_COMMENT");
                list.add(description);
                if (!description.javaType.startsWith("java.lang")) {
                    ClassDescription classDescription = new ClassDescription();
                    classDescription.javaType = description.javaType;
                    classDescription.javaTypeEasy = description.javaTypeEasy;
                    importClass.put(description.javaType, classDescription);
                }
            }
            statement.close();
            DataModel dataModel = new DataModel();
            dataModel.columns = list;
            dataModel.targetName = targetName;
            dataModel.importClass = new ArrayList<>(importClass.values());
            return dataModel;
        }

    }

    /**
     * 获取当前的数据库名称
     * @return
     */
    public String getDataBaseName() throws SQLException {
        String sql = "select database()";
        try (Statement statement = connection.createStatement()) {
            ResultSet resultSet = statement.executeQuery(sql);
            boolean success = resultSet.next();
            String tableName = resultSet.getString(1);
            return tableName;
        }
    }

    public static class ColumnDescription {
        public String columnName;

        public String dataType;

        public String jdbcType;

        public String javaType;

        public String javaTypeEasy;

        public Integer length;

        public String columnComment;
    }



}
